package com.qf.xiaomi.web.servlet;

import com.qf.xiaomi.utils.StringFinalUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;

/**
 * @Created by 马英超 on 2022/8/15 17:09
 * @author： 马英超
 * @version：v1.0
 * @description:
 */
@WebServlet("/")
public class BaseServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
           //获取请求参数
        final String method = req.getParameter("method");
        System.out.println("请求方法：\t"+method);
        HashMap<String ,Object> hashMap=new HashMap<>();//声明一个Map集合
        //获取请求URI
         String requestURI = req.getRequestURI().replace("/","");
       /* final StringBuffer requestURL = req.getRequestURL();//获取了整个资源路径*/

        //将获取的URI进行处理
         String uri = requestURI.replace(requestURI.charAt(0), (char) (requestURI.charAt(0) - 32))+"Servlet";//将URI的首字母转换为大写,并拼接
        //获取类的全限定路径
         String className = this.getClass().getPackage().getName() + "." + uri;
         System.out.println(className);
        try {
            //根据类的全限定名称获取类对象
            Class<?>   classes = Class.forName(className);
            System.out.println("classes:\t "+classes);
            if (!method.isEmpty()){
                final Method declaredMethod = classes.getDeclaredMethod(method, HttpServletRequest.class, HttpServletResponse.class);
                declaredMethod.setAccessible(true);
                System.out.println("declaredMethod:\t "+declaredMethod);
                //从hasMap中获取值，如果存在就直接取值否则就创建一个对象存入到HashMap内
                final Object obj = hashMap.computeIfAbsent(className, key -> {
                    try {
                        return classes.newInstance();
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                });
                System.out.println("obj:\t "+obj);
                //调用方法被请求类中的方法
                final String methodResult =(String) declaredMethod.invoke(obj, req, resp);


                System.out.println("methodResult:\t "+methodResult);
                     if (methodResult!=null) {
                         String url=methodResult.split(":")[1];

                         System.out.println("执行请求转发或重定向");

                         if (methodResult.startsWith("forward:")) {//请求转发
                             System.out.println("执行请求转发");
                             req.getRequestDispatcher(url).forward(req, resp);
                             System.out.println("执行请求重定向");
                         } else if (methodResult.startsWith("redirect:")) {//重定向
                             resp.sendRedirect(url);
                         } else {//将方法返回值数据直接通过流按照原路径写回去
                             resp.getWriter().write(methodResult);
                         }
                     }

            }else{
                //执行默认方法
                System.out.println("执行默认方法");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        /****************通过反射获取类对象信息*****************/
        //获取类对象
       /* final Class<BaseServlet> targetServletClass = */


    }
}
